package com.gitee.huxing.course.conc.task;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.LockSupport;

/**
 * @Description:
 * @ProjectName: JavaCourse
 * @Package: com.gitee.huxing.course.conc.task
 * @ClassName: TaskService01Impl
 * @Author: huxing
 * @DateTime: 2021-08-25 下午4:58
 */
public class TaskThreadPool19 extends AbstractTaskThreadPool{

    public static AtomicInteger atomicInteger = new AtomicInteger(0);

    public static void main(String[] args) throws InterruptedException{
        new TaskThreadPool19().task();
    }

    @Override
    public int threadTask() {
        // 线程池处理
        ExecutorService service = Executors.newFixedThreadPool(1);
        FutureTask<Integer> futureTask = new FutureTask<>(() -> CourseTask.sum());
        service.submit(futureTask);
        service.shutdown();
        try {
            while (!futureTask.isDone()){
                Thread.sleep(1000L);
            }
            atomicInteger.set(futureTask.get().intValue());
        } catch (InterruptedException | ExecutionException ex){
            ex.printStackTrace();
        }
        return atomicInteger.get();
    }

    static class CustomThread extends Thread{

        private Thread mainThread;

        public CustomThread (Thread mainThread){
            this.mainThread = mainThread;
        }

        @Override
        public void run() {
            atomicInteger.set(CourseTask.sum());
            // 处理完成线程中断
            LockSupport.unpark(mainThread);
        }
    }
}
